package com.news.web.servlet.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.news.entity.Admin;
import com.news.service.AdminService;
import com.news.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminLogInServlet
 */
@WebServlet("/AdminLogInServlet")
public class AdminLogInServlet extends HttpServlet {
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 获取传入数据
        String adminName = request.getParameter("adminName");
        String adminPass = request.getParameter("adminPass");
        
        HttpSession session=request.getSession();

        // 判断数据是否为空
        if (adminName == null || adminPass == null || "".equals(adminName) || "".equals(adminPass)) {
        	session.setAttribute("adminLogin", null);
            request.getRequestDispatcher("/admin/adminLogIn.jsp").forward(request, response);
            return; // 转发后立即返回，防止继续执行后续代码
        }

        // 创建admin类
        Admin admin = new Admin();
        admin.setAdminName(adminName);
        admin.setAdminPass(adminPass);

        // 执行查询方法
        AdminService adminService = new AdminServiceImpl();
        Admin adminLogin = adminService.selectAdmin(adminName, adminPass);
        


        // 判断是否有这个管理员
        if (adminLogin == null) {
        	session.setAttribute("adminLogin", null);
            request.getRequestDispatcher("/admin/adminLogIn.jsp").forward(request, response);
            return; // 转发后立即返回，防止继续执行后续代码
        } else {
        	session.setAttribute("adminLogin", adminLogin);
            request.getRequestDispatcher("/admin/adminIndex.jsp").forward(request, response);
            return; // 转发后立即返回，防止继续执行后续代码
        }
    }
}